package com.qianfeng.recyclerviewhfdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * Created by Administrator on 2016/12/27.
 */
public class HeaderFooterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //item类型
    public static final int HEADER = 0;
    public static final int CONTENT = 1;
    public static final int FOOTER = 2;
    //模拟数据
    public String[] texts = {"java", "python", "c++", "php", "js", "R", "Ruby", "swift"};
    private LayoutInflater mLayoutInflater;
    private int mHeaderCount = 1;//头部view的个数
    private int mFooterCount = 1;//尾部view的个数
    private Context mContext;


    public HeaderFooterAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    //内容长度
    public int getCountItemCount() {
        return texts.length;
    }

    //判断当前的item是不是HeadView
    public boolean isHeadView(int position) {
        return mHeaderCount != 0 && position < mHeaderCount;
    }

    //判断当前的item是不是footview
    public boolean isFootView(int position) {
        return mFooterCount != 0 && position >= (mHeaderCount + getCountItemCount());
    }
    //判断当前item类型


    @Override
    public int getItemViewType(int position) {

        int dataItemCount = getCountItemCount();

        if (mHeaderCount != 0 && position < mHeaderCount) {
            return HEADER;//头部
        } else if (mFooterCount != 0 && position >= (mHeaderCount + dataItemCount)) {
            return FOOTER;//底部
        } else {
            return CONTENT;
        }
    }

    //内容
    public static class ContentViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextView;

        public ContentViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv_item_text);
        }
    }

    // 头部
    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View itemView) {
            super(itemView);
        }

    }

    //底部
    public static class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View itemView) {
            super(itemView);

        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADER) {
            return new HeaderViewHolder(mLayoutInflater.inflate(R.layout.rv_header, parent, false));
        } else if (viewType == mHeaderCount) {
            return new ContentViewHolder(mLayoutInflater.inflate(R.layout.rv_item, parent, false));
        } else if (viewType == FOOTER) {
            return new FootViewHolder(mLayoutInflater.inflate(R.layout.rv_footer, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {

        } else if (holder instanceof ContentViewHolder) {

            ((ContentViewHolder) holder).mTextView.setText(texts[position - mHeaderCount]);

        } else if (holder instanceof FootViewHolder) {

        }
    }

    @Override
    public int getItemCount() {
        return mHeaderCount + mFooterCount + getCountItemCount();
    }
}
